﻿using System;
using System.Threading;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnalyseCrmMana;

namespace AnalyseCRM.InteractiveData
{
    public partial class StandardCustomerAjax : System.Web.UI.Page
    {
        private string _paramValue = "", _paramType = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["paramvalue"] != null)
            {
                _paramValue = Server.UrlDecode(Request["paramvalue"].ToString());
            }
            if (Request["paramtype"] != null)
            {
                _paramType = Request["paramtype"].ToString();
            }
            Response.Write(buildHtml(_paramValue, _paramType));
        }

        public StringBuilder buildHtml(string prparamValue, string prparamType)
        {
            StringBuilder returnValue = null;
            EstimationGuestMana egmana = null;
            string logname = CurSessionUser.curUser;
            switch (prparamType)
            {
                #region InsertEstimationGuest
                case "InsertEstimationGuest":
                    returnValue = new StringBuilder();
                    egmana = new EstimationGuestMana();
                    if (egmana.DeleteEstimatGuestForUser(logname) < 0)
                    {
                        returnValue.Append("NONE");
                    }
                    string[] estimat = prparamValue.Split(',');
                    int insertrow = 0;
                    foreach (string guest in estimat)
                    {
                        string perguest = logname+"|" + guest;
                        if (egmana.AndNewEstimationGuest(perguest.Split('|')) > 0)
                        {
                            insertrow++;
                        }
                    }
                    if (insertrow == estimat.Length)
                    {
                        returnValue.Append("SUCCESS");
                    }
                    else
                    {
                        returnValue.Append("NONE");
                    }
                    break;
                #endregion
                #region BuilderChartF01901
                case "BuilderChartF01901":
                    string chartno = prparamValue;
                    returnValue = new StringBuilder();
                    ChartBuildMana chartbm = new ChartBuildMana().getChartBuild(chartno, logname);
                    if (chartbm != null)
                    {
                        returnValue.Append(JsonHelper.ObjectToJson(chartbm));
                    }
                    else
                    {
                        returnValue.Append("NONE");
                    }
                    break;
                #endregion
                #region BuilderChartF01902
                case "BuilderChartF01902":
                    string[] chartnotwo = prparamValue.Split('|');
                    returnValue = new StringBuilder();
                    ChartBuildMana chartbmtwo = new ChartBuildMana().getChartBuild(chartnotwo[0], logname, chartnotwo[1]);
                    if (chartbmtwo != null)
                    {
                        returnValue.Append(JsonHelper.ObjectToJson(chartbmtwo));
                    }
                    else
                    {
                        returnValue.Append("NONE");
                    }
                    break;
                #endregion
                case "getEstimationGuestList":
                    //Thread.Sleep(10000);
                    returnValue = new StringBuilder();
                    egmana = new EstimationGuestMana();
                    IList<EstimationGuestMana> egmanalist = egmana.GetEstimationGuestList(prparamValue);
                    int egcount = egmana.GetEstimationGuestCount(prparamValue);
                    if (egmanalist != null && egmanalist.Count > 0)
                    {
                        returnValue.Append(JsonHelper.ListToJson(egmanalist));
                        returnValue.Append("$" + egcount);
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                case "exportEstimationGuest":
                    returnValue = new StringBuilder();
                    egmana = new EstimationGuestMana();
                    IList<EstimationGuestMana> egmanalistex = egmana.GetAllEstimationGuest(prparamValue);
                    if (egmanalistex != null && egmanalistex.Count > 0)
                    {
                        returnValue.Append(JsonHelper.ListToJson(egmanalistex));
                        returnValue.Append("$" + egmanalistex.Count);
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                #region 进行资产估算
                case "builderEstimationGues":
                    returnValue = new StringBuilder();
                    egmana = new EstimationGuestMana();
                    int rowcount = egmana.builderEstimationGues("F01901", logname, 0);
                    for (int i = 1; i <= 7; i++)
                    {
                        rowcount = rowcount + egmana.builderEstimationGues("F01902", logname, i);
                    }
                    if (rowcount > 0)
                    {
                        returnValue.Append("SUCCESS");
                    }
                    else
                    {
                        returnValue.Append("NONE");
                    }
                    break;
                #endregion
                #region 是否存在图表
                case "IsExistsEstimationChart":
                    returnValue = new StringBuilder();
                    egmana = new EstimationGuestMana();
                    int estcount1 = egmana.IsExistsEstimationChart("F01901", logname);
                    int estcount2 = egmana.IsExistsEstimationChart("F01902", logname);
                    int allest = estcount1 + estcount2;
                    returnValue.Append(allest.ToString());
                    break;
                #endregion
                #region 是否存在评估客户
                case "IsExistsEstimationGuest":
                    returnValue = new StringBuilder();
                    egmana = new EstimationGuestMana();
                    int estcount = egmana.IsExistsEstimationGuest(logname);
                    returnValue.Append(estcount.ToString());
                    break;
                #endregion
                default:
                    break;
            }
            return returnValue;

        }  
    }
}